package custom.base.data;

import android.content.Context;

import com.hjq.toast.ToastUtils;

import custom.base.utils.TxtUtil;
import custom.base.utils.store.PreferencesManager;

import java.util.HashMap;

import static custom.base.utils.store.ConstantsPreference.PRE_LAUNCH_MODE;


/**
 * Created by luozi on 2015/12/29.
 */
public class Global implements Mode, GlobalData {
    /**
     * 单例模式
     */
    private static Global instance = null;

    /**
     * 开发模式，此模式影响后台的输出，及前台显示
     */
    public Launch launchMode = Launch.RELEASE;
    /**
     * 控制模式，此模式影响app的可控性调节
     */
    public Control controlMode = Control.RELEASE;
    /**
     * 是否使用强力服务模式
     */
    public boolean STICK_SERVICE = false;
    /**
     * app 调试标记头
     */
    private String APP_DEBUG_HEADER = "dianzhuang";

    private HashMap<String, Object> globalData = new HashMap<>();

    /**
     * 单例模式
     */
    public static Global getInstance() {
        synchronized (Global.class) {
            if (instance == null)
                instance = new Global();
            return instance;
        }
    }

    /**
     * 得到当前的开发模式
     */
    public Launch getLaunchMode() {
        return launchMode;
    }

    /**
     * 初始化得到当前的开发模式
     */
    public void initLaunchMode(Context context) {
        String name = PreferencesManager.getInstance(context).getString(PRE_LAUNCH_MODE, Launch.RELEASE.name());
        if (TxtUtil.isEmpty(name)) {//默认为发布模式
            launchMode = Launch.RELEASE;
            ToastUtils.setDebugMode(false);
        } else {
            launchMode = Launch.getByName(name);
            ToastUtils.setDebugMode(isDebugMode());
        }
    }

    /**
     * 是否是debug模式
     */
    public boolean isDebugMode() {
        return launchMode == Launch.DEBUG;
    }

    /**
     * 是否是开发者模式
     */
    public boolean isReleaseMode() {
        return launchMode == Launch.RELEASE;
    }


    /**
     * 设置当前的运行模式
     */
    public void setLaunchMode(Launch launchMode, Context context) {
        this.launchMode = launchMode;
        PreferencesManager.getInstance(context).putString(PRE_LAUNCH_MODE, launchMode.name());
        ToastUtils.setDebugMode(isDebugMode());
    }

    /**
     * 得到当前的控制模式
     */
    public Control getControlMode() {
        return controlMode;
    }

    /**
     * 设置当前的运行模式
     */
    public void setControlMode(Control conrolMode) {
        this.controlMode = conrolMode;
    }

    public String getAPP_DEBUG_HEADER() {
        return APP_DEBUG_HEADER;
    }

    public void setAPP_DEBUG_HEADER(String APP_DEBUG_HEADER) {
        this.APP_DEBUG_HEADER = APP_DEBUG_HEADER;
    }

    public boolean isSTICK_SERVICE() {
        return STICK_SERVICE;
    }

    public void setSTICK_SERVICE(boolean STICK_SERVICE) {
        this.STICK_SERVICE = STICK_SERVICE;
    }

    public Object getGlobalData(String key) {
        return globalData.get(key);
    }

    public boolean getGlobalBooleanData(String key) {
        if (globalData.get(key) == null) {
            return false;
        }
        return (boolean) globalData.get(key);
    }

    public String getGlobalStringData(String key) {
        if (globalData.get(key) == null) {
            return "";
        }
        return globalData.get(key).toString();
    }

    public int getGlobalIntData(String key) {
        if (globalData.get(key) == null) {
            return 0;
        }
        return (int) globalData.get(key);
    }

    public void putGlobalData(String key, Object value) {
        globalData.put(key, value);
    }

}
